<?php
class ProceduresController extends AppController {

	var $name = 'Procedures';
	var $helpers = array('Html', 'Form', 'Javascript', 'Ajax', 'Reqtrack');

	function index() {
		$this->Procedure->recursive = 0;
		$this->set('procedures', $this->paginate(null, array('proc_project' => $this->Session->read('id_projet'))));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Procedure.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('procedure', $this->Procedure->read(null, $id));	
	}

	function add() {
		if (!empty($this->data)) {
			$this->Procedure->create();
			$this->Procedure->saveField('proc_created_by', $_SESSION['Auth']['User']['u_username']);
			$this->Procedure->saveField('proc_created_date', date('Y-m-d'));
			$this->Procedure->saveField('proc_updated_by', $_SESSION['Auth']['User']['u_username']);
			$this->Procedure->saveField('proc_updated_date', date('Y-m-d'));
			$this->Procedure->saveField('proc_project', $this->Session->read('id_projet'));
			
			if ($this->Procedure->save($this->data)) {
				$this->Session->setFlash(__('The Procedure has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Procedure could not be saved. Please, try again.', true));
			}
		}
		$procedureTypes = $this->Procedure->ProcedureType->find('list', array ('fields' => array('ProcedureType.ptype_index', 'ProcedureType.ptype_name')));
		$phases = $this->Procedure->Phase->find('list', array ('fields' => array('Phase.phase_name')));
		$parts = $this->Procedure->Part->find('list', array ('fields' => array('Part.part_index', 'Part.part_name')));
		$versions = $this->Procedure->Version->find('list');
		$categories = $this->Procedure->Category->find('list', array ('fields' => array('Category.cat_index', 'Category.cat_name')));
		$projects = $this->Procedure->Project->find('list');
		$users = $this->Procedure->User->find('list');
		$this->set(compact('procedureTypes', 'phases', 'parts', 'versions', 'categories', 'projects', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Procedure', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			$this->Procedure->saveField('proc_updated_by', $_SESSION['Auth']['User']['u_username']);
			$this->Procedure->saveField('proc_updated_date', date('Y-m-d'));
			if ($this->Procedure->save($this->data)) {
				$this->Session->setFlash(__('The Procedure has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Procedure could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Procedure->read(null, $id);
		}
//		$procedureTypes = $this->Procedure->ProcedureType->find('list');
//		$phases = $this->Procedure->Phase->find('list');
//		$parts = $this->Procedure->Part->find('list');
//		$versions = $this->Procedure->Version->find('list');
//		$categories = $this->Procedure->Category->find('list');
//		$projects = $this->Procedure->Project->find('list');
//		$users = $this->Procedure->User->find('list');
		
		$procedureTypes = $this->Procedure->ProcedureType->find('list', array ('fields' => array('ProcedureType.ptype_index', 'ProcedureType.ptype_name')));
		$phases = $this->Procedure->Phase->find('list', array ('fields' => array('Phase.phase_name')));
		$parts = $this->Procedure->Part->find('list', array ('fields' => array('Part.part_index', 'Part.part_name')));
		$versions = $this->Procedure->Version->find('list');
		$categories = $this->Procedure->Category->find('list', array ('fields' => array('Category.cat_index', 'Category.cat_name')));
		$projects = $this->Procedure->Project->find('list', array ('fields' => array('Project.proj_index', 'Project.proj_name')));
		$users = $this->Procedure->User->find('list');
		$this->set(compact('procedureTypes','phases','parts','versions','categories','projects','users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Procedure', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Procedure->del($id)) {
			$this->Session->setFlash(__('Procedure deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
}
?>